package com.orilore.gb.biz;

import java.sql.Connection;
import java.util.List;

import com.orilore.dto.XEployeeDto;
import com.orilore.gb.dao.XEmployeeDao;
import com.orilore.gb.entity.XEmployee;
import com.orilore.gb.util.Config;
import com.orilore.gb.util.ConnFactory;

public class XEmployeeBiz {
	
	private XEmployeeDao dao = new XEmployeeDao();
	
	
	public boolean UpdateEmployeeBiz(int id,String ename,int sex,int age,double salary,String address,int did) {
		
		boolean flag = false;
		
		try {
			Connection conn = ConnFactory.getConnection();
			
			int result = dao.UpdateXEmployee(conn, ename, id, sex, age, salary, address, did);
			
			if(result>0) {
				
				
				flag = true;
				
			}
			
			ConnFactory.closeConnection(conn);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return flag;
		
		
	}
	
	
	
	public XEmployee  getXEmployeeById(int id) {
		
		XEmployee  xemployee = null;
		
		
		
		try {
			Connection conn = ConnFactory.getConnection();
			
			
			xemployee = dao.getXEmployeeById(conn, id);
			
			
			ConnFactory.closeConnection(conn);
			
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return xemployee;
		
		
		
		
	}
	
	
	
	public boolean AddEmployee(String ename,int sex,int age,double salary,String address,int did) {
		
		    boolean flag = false;
		    
		    
		    try {
				Connection conn = ConnFactory.getConnection();
				
				
				int result = dao.InsertXEmployee(ename, sex, age, salary, address, did, conn);
				
				
				
				if(result>0) {
					
					
					flag = true;
					
				}
				ConnFactory.closeConnection(conn);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		
		
		    
		     return flag;
		
	}
	
	
	public List<XEployeeDto>  queryPage(String ename,String sex,String beginage,String endage ,String address, String did,int curpage){
		
		
		List<XEployeeDto>  list = null;
		
		
		try {
			Connection conn = ConnFactory.getConnection();
			
			int begin = (curpage-1)*Config.pageSize;
			
			list = dao.selectPageEmployee(conn, ename, sex, beginage, endage, address, did, begin, Config.pageSize);
			
			ConnFactory.closeConnection(conn);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return list;
		
		
		
	}
	
	
	public int getpages(String ename,String sex,String beginage,String endage ,String address, String did) {
		
		
		int result = 0;
		
		try {
			Connection conn = ConnFactory.getConnection();
			
			
			int counts = dao.getQueryCounts(conn, ename, sex, beginage, endage, address, did);
			
			
			if(counts%Config.pageSize!=0) {
				
				
				result = counts/Config.pageSize+1;
				
				
			}
			
			else {
				
				
				result = counts/Config.pageSize;
				
			}
			
			ConnFactory.closeConnection(conn);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return result;
		
		
		
		
	}
	
	
   public boolean removeXEmployee(int id) {
	   
	     boolean flag = false;
	     
	     
	     try {
			Connection conn = ConnFactory.getConnection();
			
			int result = dao.deleteXEmployeeById(conn, id);
			
			
			if(result>0) {
				
				flag = true;
				
			}
			
			ConnFactory.closeConnection(conn);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	     
	     
	     
	     
	     
	     return flag;
	   
	   
	   
	   
   }
	
	
	
	
	
	
	

}
